package cn.feng.thinkInJava.a9_3_容器.深入研究;

//: containers/CollectionMethods.java
// Things you can do with all Collections.
import java.util.*;
import net.mindview.util.*;
import static net.mindview.util.Print.*;

/**
 * Collection的功能方法
 */
public class CollectionMethods {
	public static void main(String[] args) {
		Collection<String> c = new ArrayList<String>();
		//添加参数中的所有元素,只要有添加任意元素就返回true
		c.addAll(Countries.names(6));
		//没有添加元素进容器返回false
		c.add("ten");
		c.add("eleven");
		print(c);
		// Make an array from the List:
		//toArray()返回一个数组,该数组包含容器中的所有元素
		Object[] array = c.toArray();
		// Make a String array from the List:
		String[] str = c.toArray(new String[0]);
		// Find max and min elements; this means
		// different things depending on the way
		// the Comparable interface is implemented:
		//最大值
		print("Collections.max(c) = " + Collections.max(c));
		print("Collections.min(c) = " + Collections.min(c));
		// Add a Collection to another Collection
		Collection<String> c2 = new ArrayList<String>();
		c2.addAll(Countries.names(6));
		c.addAll(c2);
		print(c);
		//如果参数在容器中,则移除此元素的一个实例.如果移除成功返回true
		c.remove(Countries.DATA[0][0]);
		print(c);
		c.remove(Countries.DATA[1][0]);
		print(c);
		// Remove all components that are
		// in the argument collection:
		//移除参数中的所有元素,只要移除动作发生,就返回true
		c.removeAll(c2);
		print(c);
		c.addAll(c2);
		print(c);
		// Is an element in this Collection?
		String val = Countries.DATA[3][0];
		print("c.contains(" + val + ") = " + c.contains(val));
		// Is a Collection in this Collection?
		print("c.containsAll(c2) = " + c.containsAll(c2));
		Collection<String> c3 = ((List<String>) c).subList(3, 5);
		// Keep all the elements that are in both
		// c2 and c3 (an intersection of sets):
		c2.retainAll(c3);
		print(c2);
		// Throw away all the elements
		// in c2 that also appear in c3:
		c2.removeAll(c3);
		print("c2.isEmpty() = " + c2.isEmpty());
		c = new ArrayList<String>();
		c.addAll(Countries.names(6));
		print(c);
		c.clear(); // Remove all elements
		print("after c.clear():" + c);
	}
} /*
 * Output: [ALGERIA, ANGOLA, BENIN, BOTSWANA, BULGARIA, BURKINA FASO, ten,
 * eleven] Collections.max(c) = ten Collections.min(c) = ALGERIA [ALGERIA,
 * ANGOLA, BENIN, BOTSWANA, BULGARIA, BURKINA FASO, ten, eleven, ALGERIA,
 * ANGOLA, BENIN, BOTSWANA, BULGARIA, BURKINA FASO] [ANGOLA, BENIN, BOTSWANA,
 * BULGARIA, BURKINA FASO, ten, eleven, ALGERIA, ANGOLA, BENIN, BOTSWANA,
 * BULGARIA, BURKINA FASO] [BENIN, BOTSWANA, BULGARIA, BURKINA FASO, ten,
 * eleven, ALGERIA, ANGOLA, BENIN, BOTSWANA, BULGARIA, BURKINA FASO] [ten,
 * eleven] [ten, eleven, ALGERIA, ANGOLA, BENIN, BOTSWANA, BULGARIA, BURKINA
 * FASO] c.contains(BOTSWANA) = true c.containsAll(c2) = true [ANGOLA, BENIN]
 * c2.isEmpty() = true [ALGERIA, ANGOLA, BENIN, BOTSWANA, BULGARIA, BURKINA
 * FASO] after c.clear():[]
 */// :~
